home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / CONVERTR / RTF2HTML / SRC / RTF2HTML.TAR / rtftohtml_src / transinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-05  |  7.8 KB  |  319 lines

  1. /*
  2.  * TransInit.c -- Read in the RTF to HTML translation Tables
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stddef.h>
  8. #include "tokenscan.h"
  9. #include "rtf.h"
  10. #include "rtftohtml.h"
  11. #include "string.h"
  12.  
  13. #define SNone     0
  14. #define STMatch 1
  15. #define SPMatch 2
  16. #define STTags    3
  17. #define SPTags    4
  18.  
  19. char *
  20. StrToken(file,lineno)
  21. char * file;
  22. int lineno;
  23. {
  24.     char * token;
  25.     if((token = TSScan ()) == (char *) NULL){
  26.         RTFPanic ("In file %s, Not enough fields in table at line:%d",
  27.         file,lineno);
  28.         }
  29.     return(token);
  30. }
  31. char * 
  32. mystrdup(ptr)
  33. char *ptr;
  34. {
  35.     char * ptr2;
  36.     int i,j;
  37.     int isesc=0;
  38.     int mlen;
  39.     mlen=strlen(ptr)+1;
  40.     if((ptr2= (char *) malloc((int) sizeof(char)*(mlen)))==NULL){
  41.         RTFPanic ("Memory Allocation Failed");
  42.     }
  43.     for(i=0,j=0;i<mlen-1;i++){
  44.         if(isesc){
  45.             if(ptr[i]=='n'){
  46.                 ptr2[j++]='\n';
  47.             } else if (ptr[i]=='t'){
  48.                 ptr2[j++]='\t';
  49.             } else {
  50.                 ptr2[j++]='\\';
  51.                 ptr2[j++]=ptr[i];
  52.             }
  53.             isesc=0;
  54.         } else if(ptr[i]=='\\'){
  55.             isesc=1;
  56.         } else {
  57.             ptr2[j++]=ptr[i];
  58.         }
  59.     }
  60.     ptr2[j]='\0';
  61.     if(j>=mlen){
  62.         RTFPanic ("Error - overwrote string");
  63.     }
  64.     return(ptr2);    
  65. }
  66.  
  67. long atobmask(str, file, lineno)
  68. char *str;
  69. char * file;
  70. int lineno;
  71. {
  72.     long bmask=0;
  73.     char *str2=str;
  74.     while(*str2!='\0'){
  75.         bmask=bmask<<1;
  76.         if(*str2=='1')bmask++;
  77.         else if(*str2!='0'){
  78.             RTFPanic ("In file %s, Invalid bitmask %s at line:%d",
  79.             str,file,lineno);
  80.         }
  81.         str2++;
  82.     }
  83.     return(bmask);
  84. }
  85. void
  86. AddRec(len,alloc,ptr,size)
  87. int *len;
  88. int *alloc;
  89. void **ptr ;
  90. size_t size;
  91. {
  92.     if(*len==*alloc){
  93.         *alloc+=10;
  94.         if(*ptr==NULL){
  95.             *ptr= (void *) malloc(size**alloc);
  96.         } else {
  97.             *ptr= (void *) realloc(*ptr,size**alloc);
  98.         }
  99.         if(*ptr==NULL){
  100.             RTFPanic ("Memory Allocation Failed (%d bytes)",size**alloc);
  101.         }
  102.     }
  103.     (*len)++;
  104. }
  105. int TransInit(file)
  106. char *file;
  107. {
  108.     FILE    *f;
  109.     char    buf[512];
  110.     TStyle_typ    bmask;
  111.     TSScanner    scanner;
  112.     char *scanDelim;
  113.     char *scanEscape;
  114.     int State=SNone;
  115.     int i;
  116.     int transver=0;
  117.     char * token;
  118.     int lineno=0;
  119.     
  120.     /* First Free up any definitions that may be there */
  121.     for(i=0;i<TMatchLen;i++){
  122.         free(TMatchArr[i].Font);
  123.     }
  124.     TMatchLen=0;
  125.     for(i=0;i<PMatchLen;i++){
  126.         free(PMatchArr[i].PStyle);
  127.     }
  128.     PMatchLen=0;
  129.     for(i=0;i<PTagLen;i++){
  130.         free(PTagArr[i].Name);
  131.         free(PTagArr[i].StartTag);
  132.         free(PTagArr[i].EndTag);
  133.         free(PTagArr[i].Col2Tag);
  134.         free(PTagArr[i].TabTag);
  135.         free(PTagArr[i].ParTag);
  136.     }
  137.     PTagLen=0;
  138.     for(i=0;i<TTagLen;i++){
  139.         free(TTagArr[i].StartTag);
  140.         free(TTagArr[i].EndTag);
  141.     }
  142.     TTagLen=0;
  143.     
  144.     if ((f = RTFOpenLibFile (file, "r")) == (FILE *) NULL)
  145.         return (0);
  146.  
  147.  
  148.     /*
  149.      * Set the list of delimiters to "," while reading the
  150.      * file.  Restore it later.
  151.      */
  152.     TSGetScanner (&scanner);
  153.     scanDelim=scanner.scanDelim;
  154.     scanner.scanDelim = ",\n \t";
  155.     scanEscape = scanner.scanEscape;
  156.     scanner.scanEscape = "";
  157.     TSSetScanner (&scanner);
  158.         
  159.     /* read file */
  160.  
  161.     while (fgets (buf, (int) sizeof (buf), f) != (char *) NULL)
  162.     {
  163.         lineno++;
  164.         if(buf[0] == '#')    /* skip comment lines */
  165.             continue;
  166.         TSScanInit (buf);
  167.         if(buf[0]== '.'){
  168.             if((token = TSScan ()) == (char *) NULL)
  169.                 continue;    /* skip blank lines */
  170.             if(strcmp(token,".Version")==0){
  171.                 transver=atoi(StrToken(file,lineno));
  172.                 continue;
  173.             }
  174.             if(strcmp(token,".TMatch")==0){
  175.                 State=STMatch;
  176.                 if(TMatchLen>0){
  177.                     RTFPanic ("Duplicate Definition of %s in: %s at %d",
  178.                     ".TMatch",file,lineno);
  179.                 }
  180.                 continue;
  181.             } else if(strcmp(token,".PMatch")==0) {
  182.                 State=SPMatch;
  183.                 if(PMatchLen>0){
  184.                     RTFPanic ("Duplicate Definition of %s in: %s at %d",
  185.                     ".PMatch",file,lineno);
  186.                 }
  187.                 continue;
  188.             } else if(strcmp(token,".PTag")==0) {
  189.                 State=SPTags;
  190.                 if(PTagLen>0){
  191.                     RTFPanic ("Duplicate Definition of %s in: %s at %d",
  192.                     ".PTag",file,lineno);
  193.                 }
  194.                 continue;
  195.             } else if(strcmp(token,".TTag")==0) {
  196.                 State=STTags;
  197.                 if(TTagLen>0){
  198.                     RTFPanic ("Duplicate Definition of %s in: %s at %d",
  199.                     ".TTag",file,lineno);
  200.                 }
  201.                 continue;
  202.             } else {
  203.                 RTFPanic ("Invalid Control Word %s in: %s at %d",token,file,lineno);
  204.             }
  205.         } 
  206.         if((token = TSScan ()) == (char *) NULL)
  207.             continue;    /* skip blank lines */
  208.         if(transver != Htran){
  209.             RTFPanic ("The version of html-trans is incompatible with this release of rtftohtml.");
  210.         }
  211.  
  212.         switch(State){
  213.         case STMatch:
  214.                 AddRec(&TMatchLen,&TMatchAlloc,(void **)&TMatchArr,sizeof(TMatchRec));
  215.                 TMatchArr[TMatchLen-1].Font=mystrdup(token);
  216.                 TMatchArr[TMatchLen-1].FSize=atoi(StrToken(file,lineno));
  217.                 TMatchArr[TMatchLen-1].TStyle=atobmask(StrToken(file,lineno),file,lineno);
  218.                 TMatchArr[TMatchLen-1].TMask=atobmask(StrToken(file,lineno),file,lineno);
  219.                 token=StrToken(file,lineno);
  220.                 if(strcmp(token,"_Discard")==0){
  221.                     TMatchArr[TMatchLen-1].TTidx=MTDiscard;
  222.                 } else if(strcmp(token,"_Name")==0){
  223.                     TMatchArr[TMatchLen-1].TTidx=MTName;
  224.                 } else if(strcmp(token,"_HRef")==0){
  225.                     TMatchArr[TMatchLen-1].TTidx=MTHref;
  226.                 } else if(strcmp(token,"_Hot")==0){
  227.                     TMatchArr[TMatchLen-1].TTidx=MTHot;
  228.                 } else if(strcmp(token,"_FootNote")==0){
  229.                     TMatchArr[TMatchLen-1].TTidx=MTFootNote;
  230.                 } else if(strcmp(token,"_Literal")==0){
  231.                     TMatchArr[TMatchLen-1].TTidx=MTLiteral;
  232.                 } else {
  233.                     for(i=0;i<TTagLen;i++){
  234.                         if(strcmp(token,TTagArr[i].Name)==0){
  235.                             TMatchArr[TMatchLen-1].TTidx=i;
  236.                             break;
  237.                         }
  238.                     }
  239.                     if(i==TTagLen){
  240.                         RTFPanic ("Tag Name %s not in .TTag table, file:%s line %d",
  241.                         token,file,lineno);
  242.                     }
  243.                 }
  244.                 break;
  245.         case SPMatch:
  246.                 AddRec(&PMatchLen,&PMatchAlloc,(void **)&PMatchArr,sizeof(PMatchRec));
  247.                 PMatchArr[PMatchLen-1].PStyle=mystrdup(token);
  248.                 PMatchArr[PMatchLen-1].NestLev=atoi(StrToken(file,lineno));
  249.                 token=StrToken(file,lineno);
  250.                 if(strcmp(token,"_Discard")==0){
  251.                     PMatchArr[PMatchLen-1].PTidx=MTDiscard;
  252.                 } else if(strcmp(token,"_Name")==0){
  253.                     PMatchArr[PMatchLen-1].PTidx=MTName;
  254.                 } else if(strcmp(token,"_HRef")==0){
  255.                     PMatchArr[PMatchLen-1].PTidx=MTHref;
  256.                 } else if(strcmp(token,"_Hot")==0){
  257.                     PMatchArr[PMatchLen-1].PTidx=MTHot;
  258.                 } else if(strcmp(token,"_FootNote")==0){
  259.                     PMatchArr[PMatchLen-1].PTidx=MTFootNote;
  260.                 } else if(strcmp(token,"_Literal")==0){
  261.                     PMatchArr[PMatchLen-1].PTidx=MTLiteral;
  262.                 } else {
  263.                     for(i=0;i<PTagLen;i++){
  264.                         if(strcmp(token,PTagArr[i].Name)==0){
  265.                             PMatchArr[PMatchLen-1].PTidx=i;
  266.                             break;
  267.                         }
  268.                     }
  269.                     if(i==PTagLen){
  270.                         RTFPanic ("PTag Name %s not in .PTag table, file:%s line %d",
  271.                         token,file,lineno);
  272.                     }
  273.                 }
  274.                 break;
  275.         case SPTags:
  276.                 AddRec(&PTagLen,&PTagAlloc,(void **)&PTagArr,sizeof(PTagRec));
  277.                 PTagArr[PTagLen-1].Name=mystrdup(token);
  278.                 PTagArr[PTagLen-1].StartTag=mystrdup(StrToken(file,lineno));
  279.                 PTagArr[PTagLen-1].EndTag=mystrdup(StrToken(file,lineno));
  280.                 PTagArr[PTagLen-1].Col2Tag=mystrdup(StrToken(file,lineno));
  281.                 PTagArr[PTagLen-1].TabTag=mystrdup(StrToken(file,lineno));
  282.                 PTagArr[PTagLen-1].ParTag=mystrdup(StrToken(file,lineno));
  283.                 PTagArr[PTagLen-1].AllowText=atoi(StrToken(file,lineno));
  284.                 PTagArr[PTagLen-1].CanNest=atoi(StrToken(file,lineno));
  285.                 PTagArr[PTagLen-1].DeleteCol1=atoi(StrToken(file,lineno));
  286.                 PTagArr[PTagLen-1].DoFold=atoi(StrToken(file,lineno));
  287.                 PTagArr[PTagLen-1].ToCLev=atoi(StrToken(file,lineno));
  288.                 break;
  289.         case STTags:
  290.                 AddRec(&TTagLen,&TTagAlloc,(void **)&TTagArr,sizeof(TTagRec));
  291.                 TTagArr[TTagLen-1].Name=mystrdup(token);
  292.                 TTagArr[TTagLen-1].StartTag=mystrdup(StrToken(file,lineno));
  293.                 TTagArr[TTagLen-1].EndTag=mystrdup(StrToken(file,lineno));
  294.                 break;
  295.         case SNone:
  296.                 RTFPanic ("Missing Control Word in %s, at line %d",file,lineno);
  297.         }
  298.     }
  299.     scanner.scanDelim = scanDelim;
  300.     scanner.scanEscape = scanEscape;
  301.     TSSetScanner (&scanner);
  302.     fclose(f);
  303.     if(TTagLen==0){
  304.         RTFPanic ("Missing table %s, in file: %s",".TTag",file);
  305.     }
  306.     if(PTagLen==0){
  307.         RTFPanic ("Missing table %s, in file: %s",".PTag",file);
  308.     }
  309.     if(TMatchLen==0){
  310.         RTFPanic ("Missing table %s, in file: %s",".TMatch",file);
  311.         /*exit(1);*/
  312.     }
  313.     if(PMatchLen==0){
  314.         RTFPanic ("Missing table %s, in file: %s",".PMatch",file);
  315.     }
  316.  
  317.     return (1);
  318. }
  319.